#coding:utf8
from urllib import request
import json,time,random





fuwuqi="23"

username="gaomao20"
password="memeda"




for c in range(1):
    #登录游戏,并取得token_uid
    def login(username,password):
        url="http://uc.hanjiang918.com:8092/index.php?&c=user&m=login&&token=&channel=106&lang=zh-cn&rand=143674622314894&u="+username+"&p="+password
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        print("登录成功")
        return str(fnew['uid'])
        
    #取得token  
    def gettoken(fuwuqi,username,password):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=login&&m=user&&token=&channel=106&lang=zh-cn&rand=143674622405396&u="+username+"&p="+password
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        print("取得token")
        return fnew['token']
    def getrole(fuwuqi,token_uid,token):
        nickname=zimu[random.randint(0,25)] + \
             str(random.randint(10,99)) + \
             zimu[random.randint(0,25)] + \
             zimu[random.randint(0,25)]
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=member&&m=select_role&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143737917907324&sex=2&name="+nickname
        request.urlopen(url)
    #跳过引导
    def skip(fuwuqi,token_uid,token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314index.php?v=0&c=member&&m=updateguidestep&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143737149898765&step=1&stop=1"
        request.urlopen(url)
        print("跳过引导")
    
    
    
    
    
    #获取武将gid
    def getgid(fuwuqi,token_uid,token,wujiang):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=muster&&m=index&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143739519493083&page=1"
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        print("获取武将%s的gid"%wujiang)
        for i in range(1,len(fnew['list'])+1):
            if fnew['list'][str(i)]['name']==wujiang:
                return fnew['list'][str(i)]['id']
        else:
            return "尚无武将%s"%wujiang
    
    #领取等级礼包
    def getgift(fuwuqi,token_uid,token,level):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=levelgift&&m=get_reward&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143676633430568&level="+str(level)
        request.urlopen(url)
        print("领取%s级的等级礼包"%level)
        
        
    #获取训练场放置位置    
    def getpid(fuwuqi,token_uid,token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=practice&&m=index&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=14367671582957"
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        print("获取训练场PID")
        return fnew['place']['1']['id']
    
    
    #新服礼包
    def xinfu(fuwuqi,token_uid,token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=newgift&&m=get_reward&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143739507777126&id=1"
        request.urlopen(url)
    
    #任务奖励
    def renwujiang(fuwuqi,token_uid,token,nu):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=mainquest&&m=index&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143739706589247"
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        x=int(fnew['list'][0]['task_id'])
        for i in range(0,nu):
            url1="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=mainquest&&m=get_task_reward&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143740765768780&id="+str(x)
            request.urlopen(url1)
            x+=1
    
    
    #开始训练
    def start(fuwuqi,token_uid,token,gid,pid):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=practice&&m=practice_start&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143676716001134&pid="+str(pid)+"&gid="+str(gid)+"&type=1"
        request.urlopen(url)
        print("开始训练")
        
    #突飞
    def tufei(fuwuqi,token_uid,token,gid):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=practice&&m=go_leap&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143640942406382&gid="+str(gid)
        while 1:
            f=request.urlopen(url).read().decode('utf8')
            fnew=json.loads(f)
            if fnew['status'] == 3:
                print("完成突飞")
                break
            if fnew['status'] == 1 and int(fnew['info']['exp'])==0:
                print("完成突飞")
                zhuansheng(fuwuqi, token_uid, token, gid)
                saodang(fuwuqi, token_uid, token, gid)
               
    #扫荡        
    def saodang(fuwuqi,token_uid,token,gid):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=practice&&m=mop&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143640942406382&gid="+str(gid)+"&times=50"
        while 1:
            f=request.urlopen(url).read().decode('utf8')
            fnew=json.loads(f)
            if int(fnew['status']) == 3 or int(fnew['status']) == 4:
                print("扫荡结束，开始突飞")
                tufei(fuwuqi, token_uid, token, gid)
                break
    
    #转生
    def zhuansheng(fuwuqi,token_uid,token,gid):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=practice&&m=turn&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143640942406382&gid="+str(gid)
        request.urlopen(url)
    
    #上阵
    def shangzhen(fuwuqi,token_uid,token,gid):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=muster&&m=go_battle&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143739520188386&gid="+gid+"&confirm=0"
        request.urlopen(url)
    #升级阵法
    def shengji(fuwuqi,token_uid,token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=matrix&&m=levelup&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143739543252112&mid=1"
        request.urlopen(url)
       
    #装备id    
    def geteid(fuwuqi,token_uid,token,equipname):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=pack&&m=index&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143676657460224"
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        for i in range(0,len(fnew['list'])):
            if fnew['list'][i]['name']==equipname:
                return fnew['list'][i]['id']
        else:
            return "找不到名为%s的装备"%equipname
    
 

    #地图列表
    def getmaplist(fuwuqi,token_uid,token,l):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=map&&m=get_scene_list&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143737172860025&l="+str(l)
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        return len(fnew['list'])
       
    def getmissionlist(fuwuqi,token_uid,token,l,s):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=map&&m=get_mission_list&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143737170974087&l="+str(l)+"&s="+str(s)
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        return len(fnew['list'])
    #推图
    def tui(fuwuqi,token_uid,token,l,s,idss):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=map&&m=action&&token_uid="+str(token_uid)+"&token="+token+"&channel=106&lang=zh-cn&rand=143676787924432&l="+str(l)+"&s="+str(s)+"&id="+str(idss)
        f=request.urlopen(url).read().decode('utf8')
        fnew=json.loads(f)
        return int(fnew['info']['win'])
        
    def issuccess(fuwuqi, token_uid, token, l, s, idss,status):
        if status <=0 and l ==1 and s==1 and idss==1:
            print("角色名创建失败")
            getrole(fuwuqi, token_uid, token) 
        if status <=0:
            print("%s-%s-%s未能通过"%(l,s,idss))
            status=tui(fuwuqi, token_uid, token, l, s, idss)
            issuccess(fuwuqi, token_uid, token, l, s, idss, status)   
        else:
            print("%s-%s-%s成功"%(l,s,idss))
    
    
    def getf(fuwuqi, token_uid, token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=map&&m=get_newresult&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143740391387725&id=1"
        request.urlopen(url)
    def tuan(fuwuqi, token_uid, token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=christmasgift&&m=get_reward&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=14374065821654&id=1"
        request.urlopen(url)
    def pei(fuwuqi,token_uid,token,gid):
        give="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=cultivate&&m=giveup&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143740420567835&gid="+str(gid)
        save="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=cultivate&&m=save&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143740421097590&gid="+str(gid)
        roll="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=0&c=cultivate&&m=roll&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=143740420408454&gid="+str(gid)+"&mode=2"
        wuli=2
        zhili=2
        tili=1
        while 1:
            f=request.urlopen(roll).read().decode("utf8")
            fnew=json.loads(f)
            if int(fnew['info']['tiliup'])+int(fnew['info']['wuliup'])+int(fnew['info']['zhiliup'])>=zhili+tili+wuli:
                wuli=int(fnew['info']['wuliup'])
                zhili=int(fnew['info']['zhiliup'])
                tili=int(fnew['info']['tiliup'])
                request.urlopen(save)
            else:
                request.urlopen(give)
            print(wuli,tili,zhili)
            if wuli >=190 and tili>= 190 and zhili>=190:
                break
            
    def heishi(fuwuqi, token_uid, token):
        url="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=%E6%82%8D%E5%B0%864.0&c=blackmarket&&m=refresh&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=144465309485591"
        f=request.urlopen(url).read().decode("utf8")
        fnew=json.loads(f)
        for i in range(0,len(fnew['list'])):
            name=fnew['list'][str(i+1)]['name']
            if name not in ['军令','银币','中级卡','初级卡','高级卡']:
                print(name)
                ids=fnew['list'][str(i+1)]['id']
                urls="http://s"+fuwuqi+".game.hanjiang918.com:1314/index.php?v=%E6%82%8D%E5%B0%864.0&c=blackmarket&&m=buy&&token_uid="+token_uid+"&token="+token+"&channel=106&lang=zh-cn&rand=144465318066844&id="+str(ids)
                request.urlopen(urls)
    token_uid=login(username, password)
    token=gettoken(fuwuqi, username, password)
    gid=getgid(fuwuqi,token_uid,token,"神吕蒙")
    pei(fuwuqi,token_uid,token,gid)
    #tufei(fuwuqi,token_uid,token,gid)
    #while 1:
      #  heishi(fuwuqi, token_uid, token)
    '''
    for l in range(5,15):
        ss=getmaplist(fuwuqi, token_uid, token, l)+1
        for s in range(1,ss):
            idsss=getmissionlist(fuwuqi, token_uid, token, l, s)+1
            print(idsss)
            for idss in range(1,idsss):
                status=tui(fuwuqi, token_uid, token, l, s, idss)
                issuccess(fuwuqi, token_uid, token, l, s, idss, status)
                
'''
